#!/usr/bin/env python
# -*- coding: utf-8 -*-


import os, os.path, sys
import md5

import tempfile
import unittest

from potherb import log
from potherb.cellstore import CellStore
from potherb.key import KeySmith
from potherb.const import COLUMN_NOT_FOUND,COLUMN_FOUND,COLUMN_DELETED,ROW_DELETED

DASH = '-'

def makeData(key) :
    return DASH.join([key] * 4)
    # makeData()


class   TestCellStore(unittest.TestCase) :
    def setUp(self) :
        self.store_dir = tempfile.mkdtemp(suffix='.dir', prefix='store_')
        log.debug('store_dir=%s', self.store_dir)
        ## setUp()

    def removeDirs(self, dir_name) :
        items = os.listdir(dir_name)
        for item in items :
            dir_item = '%s/%s' % (dir_name, item)
            if os.path.isdir(dir_item) :
                self.removeDirs(dir_item)
            else :
                #log.debug('CONTENT of file %s', dir_item)
                #for line in file(dir_item) :
                #    log.debug('%s', line.strip())
                log.debug('remove file %s', dir_item)
                os.remove(dir_item)
        if os.path.exists(dir_name) :
            log.debug('remove directory %s', dir_name)
            os.removedirs(dir_name)
        ## removedirs()

    def tearDown(self) :
        if os.path.exists(self.store_dir) :
            self.removeDirs(self.store_dir)
        ## tearDown()

    def test_001(self) :
        '''test that even without available store file, CellStore can be iteratable, but raise StopIteration 
        '''
        store = CellStore(self.store_dir)

        count = 0
        for key,data in store :         # this will call __iter__()
            count += 1
        self.assertEqual(0, count)

        store_iterator = iter(store)    # this would call __iter__()
        self.assertRaises(StopIteration, store_iterator.next)

        del store
        ## test_001()

    def test_002(self) :
        '''test that CellStore can store data in ASCEND order
        '''
        # write data to CellStore
        store_write = CellStore(self.store_dir)
        store = store_write
        
        for i in range(0, 32, 1) :
            key = '%05X' % i
            data = '%05X--DATA' % i
            store.append(key, data)

        store.freeze()
        store = None
        del store_write

        # iterate data over CellStore
        store_iter = CellStore(self.store_dir)
        store = store_iter

        iterator = iter(store)
        for i in range(0, 32, 1) :
            key_expect = '%05X' % i
            data_expect = '%05X--DATA' % i
            key,data = iterator.next()
            self.assertEqual(key, key_expect)
            self.assertEqual(data, data_expect)

        store = None
        del store_iter
        ## test_002()

    def test_003(self) :
        '''test that CellStore can store data in DESCEND order
        '''
        # write data to CellStore
        store_write = CellStore(self.store_dir)
        store = store_write
        
        for i in range(64, 0, -1) :
            key = '%05X' % i
            data = '%05X--DATA' % i
            store.append(key, data)

        store.freeze()
        store = None
        del store_write

        # iterate data over CellStore
        store_iter = CellStore(self.store_dir)
        store = store_iter

        iterator = iter(store)
        for i in range(64, 0, -1) :
            key_expect = '%05X' % i
            data_expect = '%05X--DATA' % i
            key,data = iterator.next()
            self.assertEqual(key, key_expect)
            self.assertEqual(data, data_expect)

        store = None
        del store_iter
        ## test_003()

    def Ntest_002(self) :
        cell = CellStore(self.store_dir)
        
        timestamp = 1000
        for row_key in ('foo', 'baz', 'bar') :
            data = '%s--DATA--%d' % (row_key, timestamp)
            for flag in (KeySmith.FLAG_INSERT, KeySmith.FLAG_DELETE_COLUMN_FAMILY, KeySmith.FLAG_DELETE_ROW) :
                smith = KeySmith(row_key=row_key, flag=flag, timestamp=timestamp)
                cell.insert(row_key, smith.key, data)

            timestamp += 1

        for row_key in ('foo', 'baz', 'bar') :
            smith = KeySmith(row_key=row_key)
            column_key = smith.getColumnKey()
            _status,_data,_timestamp = cell.findColumn(row_key, column_key)
            self.assertEqual(ROW_DELETED, _status)
            self.assertNotEqual(COLUMN_DELETED, _status)

        for row_key in ('fooN', 'bazN', 'barN') :
            smith = KeySmith(row_key=row_key)
            column_key = smith.getColumnKey()
            _status,_data,_timestamp = cell.findColumn(row_key, column_key)
            self.assertEqual(COLUMN_NOT_FOUND, _status)

        del cell
        ## test_001()


    ## class TestCellStore

#######################################################################

def test_suite():
    suite = unittest.TestSuite()

    suite.addTest(unittest.makeSuite(TestCellStore))

    return suite

if  __name__ == '__main__' :
    #log.setLogPath('/dev/null')
    log.enableDebug()

    unittest.main()

